clear


message = 'Enter data folder \n';
folData = input( message , 's');

message = 'Enter first part of file name \n';
nameFile = input( message , 's');
namekey = strcat(nameFile,'*');

disp('Define abscissa');
disp('1: X')
disp('2: Y')
Xaxis = input('?');

message = 'Fix ends? (y/n) \n';
ends = input( message , 's');

if ends == 'y' || ends == 'Y'
    
    clipSides = 1;
    
else
    clipSides = 0;
end

%generates folders to store results
[folRoot,folResults] = newfolder(folData,'results');

%generates a vector with the name of the files to process
[PathFiles] = filesVector(folData,namekey);
n = 1;
nc1 = 0;

for kk = 1 : length(PathFiles)
    path = char(PathFiles(kk));
    U = vectorLoader(path);
    nc = length( U(1 , :) );
    
    if kk == 1
        Uc = U;
        nc0 = nc;
    else
        if nc == nc0
            Uc = [Uc ; U]; %#ok<*AGROW>
        elseif n == 1
            Uc2 = U;
            nc1 = nc;
            n = 2;
        elseif n == 2
            Uc2 = [Uc2 ; U];
        end
    end
end

if nc0 == 18 && nc1 ~= 0
    L = [Uc(:,Xaxis) , Uc(:,3) , Uc(:,8) , Uc(:,13) ; ...
        Uc2(:,Xaxis) , Uc2(:,3) , Uc2(:,7) , Uc2(:,11)];
elseif nc0 == 11 && nc1 ~=0
    L = [Uc(:,Xaxis) , Uc(:,3) , Uc(:,7) , Uc(:,11) ; ...
        Uc2(:,Xaxis) , Uc2(:,3) , Uc2(:,8) , Uc2(:,13)];
elseif nc1 == 0 && nc0 == 18
    L = [Uc(:,Xaxis) , Uc(:,3) , Uc(:,8) , Uc(:,13) ];
elseif nc1 == 0 && nc0 == 11
    L = [Uc(:,Xaxis) , Uc(:,3) , Uc(:,7) , Uc(:,11) ];
end

Lii = sortrows(L,1);
S = size(Lii);

Lii(:,1) = Lii(:,1) - min(Lii(:,1));

if clipSides == 1
    
    LX = repmat(Lii(:,1) , [1 , S(2) - 1]); 
    LL = 2 * (Lii(: , 2 : end) - LX);
    LR = 2 * (Lii(: , 2 : end) - ( max(max(LX)) - LX ));


    for k = 1 : S(2) - 1

        Left = find( abs( Lii(:,k + 1) - LL(:,k)) - min(abs( Lii(:,k + 1) - LL(:,k) )) == 0, 1 , 'first' );
        Right = find( abs( Lii(:,k + 1) - LR(:,k)) - min(abs( Lii(:,k + 1) - LR(:,k) )) == 0, 1 , 'first' );
        Lii(1 : Left , k + 1) = LL( 1 : Left , k);
        Lii(Right : end , k + 1) = LR( Right : end , k);

    end
    
end

filename = strcat('\' , nameFile, '.dat');
ti = num2str(S(1));
heading = strcat('ZONE I = ' , ti , ' F = POINT');
saver(folResults,filename,heading,Lii)

plot(Lii(:,1),Lii(:,2),Lii(:,1),Lii(:,3),Lii(:,1),Lii(:,4));